from wtforms import Form
from flask import request

from app.libs.error_code import ParameterException


class BaseFrom(Form):
    def __init__(self, re_data=None):
        if request.method == 'GET':
            data = dict(request.args)
        else:
            data = request.json
        if re_data:
            data = re_data
        super(BaseFrom, self).__init__(data=data)

    def validate_and_api(self):
        validate = super(BaseFrom, self).validate()
        if not validate:
            raise ParameterException(msg=str_data(self.errors))
        return self


def str_data(person_list):
    if isinstance(person_list, str):
        return person_list
    return ", ".join([str(person) + ": " + str(person_list[person][0]) for person in person_list])
